package project.suntray.com.core.net;

import java.io.File;
import java.util.List;
import java.util.WeakHashMap;

import commerce.zhangyang.com.core.net.callback.*;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;

/**
 * Created by zhangyang on 2017/12/14 07:35.
 * version 1
 * 定义 请求的客户端
 * 封装的 所有请求因素组合
 *
 */
public final class RestClient {
    /**
     * 参数集合
     */
    private final WeakHashMap<String, Object> params = RestCreator.getParams();

    /**
     * 访问的url
     */
    private final String URL;

    /**
     * 文件对象
     */
    private final File File;

    private final List<File> FILELIST;
    /**
     * 下载的目录
     */
    private final String downloadDir;

    /**
     * 回调器
     */
    private final ISuccess iSuccess;
    private final IFailure iFailure;
    private final IError iError;
    private final IRequest iRequest;
    private final RequestBody requestBody;


    public RestClient(String url, java.io.File file, List<java.io.File> filelist, String downloadDir, ISuccess success, IFailure failure, IError iError, IRequest iRequest, RequestBody requestBody) {
        URL = url;
        File = file;
        FILELIST = filelist;
        this.downloadDir = downloadDir;
        iSuccess = success;
        iFailure = failure;
        this.iError = iError;
        this.iRequest = iRequest;
        this.requestBody = requestBody;
    }

    /**
     * 获取对应的构造者
     * @return
     */
    public static RestClientBuilder builder(){
        return  new RestClientBuilder();
    }


    /**
     * 进行请求接口信息
     * @param httpMethod
     */
    private void request(HttpMethod httpMethod) {
        //获取对应的service
        RestService restService=RestCreator.getRestService();
        Call<String> call=null;

        //判断出 回调是否为空
        if(iRequest!=null){
            iRequest.onRequestStart();
        }

        //如果有加载器 调用加载动画
        switch (httpMethod) {
            case GET:
                //get方式
                call=restService.get(URL,params);
                break;

            case POST:
                call=restService.post(URL,params);
                break;

            case POST_RAW:
                call=restService.postRaw(URL,requestBody);
                break;

            case PUT:
                call=restService.put(URL,params);
                break;

            case PUT_RAW:
                call=restService.putRaw(URL,requestBody);
                break;

            case DELETE:
                call=restService.delete(URL,params);
                break;

            case UPLOAD_SINGLE:
                //先进行获取requestBody
                //multipart/form-data ,application/otcet-steam
                RequestBody requestBody=RequestBody.create(MediaType.parse(MultipartBody.FORM.toString()),File);
                //获取对应的
                MultipartBody.Part multipartPart=MultipartBody.Part.create(requestBody);
                call=restService.upload(URL,multipartPart);
                break;

            case UPLOAD_MULTI:
                //第二种方式: 多文件上传
                //创建构建者
                MultipartBody.Builder builder=new MultipartBody.Builder();
                //遍历文件集合
                for(File file:FILELIST){
                    //创建 请求对象 requestBody
                    RequestBody requestBody1=RequestBody.create(MediaType.parse("application/otcet-stream"),file);
                    builder.addFormDataPart("file",file.getName(),requestBody1);
                }
                builder.setType(MultipartBody.FORM);
                call=restService.uploadMultiFile(URL,builder.build());
                break;
        }

        //设置回调
        call.enqueue(getRequestCallBack());
    }

    /**
     * 获取对应的回调信息
     * @return
     */
    public Callback<String> getRequestCallBack() {
        return new ReqeustsCallBack(iRequest, iSuccess, iFailure,iError);
    }

    /**
     * 对应请求get方法
     */
    public final void get(){
        request(HttpMethod.GET);
    }

    /**
     * 对应请求post方法
     */
    public final void post(){
        if(requestBody!=null){
            request(HttpMethod.POST);
        }else{
            if(params!=null){
                request(HttpMethod.POST_RAW);
            }
        }
    }

    /**
     * 对应请求post方法
     */
    public final void put(){
        if(requestBody!=null){
            request(HttpMethod.PUT);
        }else{
            if(params!=null){
                request(HttpMethod.PUT_RAW);
            }
        }
    }

    /**
     * 删除请求
     */
    public final void delete(){
        request(HttpMethod.DELETE);
    }

    /**
     * 上传文件 请求
     */
    public final void upload(){
        if(File!=null){
            request(HttpMethod.UPLOAD_SINGLE);
        }else{
            request(HttpMethod.UPLOAD_MULTI);
        }
    }
}
